// utils/util.js
const GPU_DATABASE = [
  { name: "RTX 3090 / RTX 4080", vram: 16, level: "消费级" },
  { name: "RTX 4090 / RTX A6000", vram: 24, level: "消费级旗舰" },
  { name: "A100 (PCIe)", vram: 40, level: "数据中心级" },
  { name: "A100 (SXM4) / H100 (PCIe)", vram: 80, level: "数据中心级旗舰" },
  { name: "H100 (SXM5) / A100 80GB", vram: 120, level: "超算级" }
];

function getGPURecommendation(requiredVRAM) {
  const requiredWithRedundancy = requiredVRAM * 1.1;

  const suitableGPUs = GPU_DATABASE
    .filter(gpu => gpu.vram >= requiredWithRedundancy)
    .sort((a, b) => a.vram - b.vram);

  if (suitableGPUs.length === 0) {
    const maxGPU = GPU_DATABASE.sort((a, b) => b.vram - a.vram)[0];
    const requiredCards = Math.ceil(requiredWithRedundancy / maxGPU.vram);
    return `需求过大（${requiredVRAM.toFixed(1)}GB），推荐 ${requiredCards}×${maxGPU.name} (${maxGPU.vram}GB)`;
  }

  const consumerGPU = suitableGPUs.find(gpu => gpu.level.includes("消费级"));
  const dataCenterGPU = suitableGPUs.find(gpu => gpu.level.includes("数据中心"));

  if (consumerGPU && dataCenterGPU) {
    return `需求 ${requiredVRAM.toFixed(1)}GB，推荐：\n消费级 ${consumerGPU.name} (${consumerGPU.vram}GB) | 数据中心级 ${dataCenterGPU.name} (${dataCenterGPU.vram}GB)`;
  } else {
    return `需求 ${requiredVRAM.toFixed(1)}GB，推荐：${suitableGPUs[0].name} (${suitableGPUs[0].vram}GB)`;
  }
}

module.exports = { getGPURecommendation };